﻿/*
Problem 2 – Special Value
 * 
You are given N number of rows. Each row contains a different number of columns. Each cell in a row contains a negative number or an index of a cell on the next row, i.e. rows[row][column] holds an index of cell in [row+1]. The row after the last row is the first row. If the number of columns on the next row is C, each of the cells on the current row are in the range [-C, C-1] inclusive.
A path in the rows is the max number of cells that can be passed, starting from any of the cells on the first row, and following the pattern above (i.e. each cell on row R, holds a negative number or an index on row R+1). The path ends when the path reaches an already visited cell in this path, or the number in the cell is negative.
A special value is the sum of the path + the absolute value of a negative number reached. If a path reaches a visited cell, this path cannot get a special value.
Your task is to find the biggest special value using the given rows.
Input
The input data should be read from the console. 
On the first line you will be given the number N.
On the next N lines you will be given the numbers in each row, separated with ", " (comma and space).
Output
The output data should be printed on the console.
The output should contain only the maximal special number.
Constraints
•	The rows will be between 1 and 1000 inclusive.
•	The cells on each row will be between 1 and 1000 inclusive.
•	Allowed working time for your program: 0.5 seconds. Allowed memory: 16 MB.

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Problem2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            List<string> stringRows = new List<string>(n);
            for (int i = 0; i < n; i++)
            {
                stringRows.Add(Console.ReadLine());
            }
            int[][] jagged = new int[n][];
            for (int i = 0; i < n; i++)
            {
                string[] numbers = stringRows[i].Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                jagged[i] = new int[numbers.Length];
                for (int j = 0; j < numbers.Length; j++)
                {
                    jagged[i][j] = int.Parse(numbers[j]);
                }
            }
            int maxResult = 0;
            int cellNumber;
            for (int i = 0; i < jagged[0].Length; i++)
            {
                int row = 0;
                int pathCounter = 0;
                int tempMaxResult = 0;
                int lastPosition = 0;
                cellNumber = i;
                do
                {
                    pathCounter++;
                    if (jagged[row][cellNumber] < 0)
                    {
                        tempMaxResult = pathCounter + (-1) * jagged[row][cellNumber];
                        break;
                    }
                    else
                    {
                        cellNumber = jagged[row][cellNumber];
                        if (row + 1 > n - 1) row = 0;
                        else row++;
                    }
                    if (pathCounter % n == 1)
                    {
                        if (cellNumber == lastPosition) break;
                        else lastPosition = cellNumber;
                    }
                } while (true);
                if (tempMaxResult > maxResult) maxResult = tempMaxResult;
            }
            Console.WriteLine(maxResult);
        }

    }
}
